package com.itheima.controller.sms;

import com.itheima.api.sms.SmsFlashPromotionProductRelationService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.dto.SmsFlashPromotionProductRelationDto;
import com.itheima.vo.pojo.SmsFlashPromotionProductRelation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("flashProductRelation")
public class SmsFlashPromotionProductRelationController {

    @DubboReference
    private SmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    /**
     * 分页查询相关商品及限时购促销信息
     *
     * @param flashPromotionId        限时购id
     * @param flashPromotionSessionId 限时购场次id
     */
    @GetMapping("list")
        public AjaxResult list(@RequestParam(value = "flashPromotionId") Long flashPromotionId,
                               @RequestParam(value = "flashPromotionSessionId") Long flashPromotionSessionId,
                               @RequestParam(value = "pageSize") Integer pageSize,
                               @RequestParam(value = "pageNum") Integer pageNum){

        TableDataInfo tableDataInfo = smsFlashPromotionProductRelationService.listAll(pageNum, pageSize, flashPromotionId, flashPromotionSessionId);


        return AjaxResult.success(tableDataInfo);
    }

    //批量选择商品添加关联
    @PostMapping("create")
    public AjaxResult create(@RequestBody List<SmsFlashPromotionProductRelation> smsFlashPromotionProductRelation){

        for (SmsFlashPromotionProductRelation flashPromotionProductRelation : smsFlashPromotionProductRelation) {
            smsFlashPromotionProductRelationService.save(flashPromotionProductRelation);
        }

        return AjaxResult.success("添加关联成功");
    }

    //删除关联
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){

        smsFlashPromotionProductRelationService.removeById(id);

        return AjaxResult.success("删除关联成功");
    }

    //获取管理商品促销信息
    @GetMapping("{id}")
    public AjaxResult findById(@PathVariable Long id){

        SmsFlashPromotionProductRelation s = smsFlashPromotionProductRelationService.getById(id);

        return AjaxResult.success(s);
    }

    //修改关联信息
    @PostMapping("update/{id}")
    public AjaxResult update(@PathVariable Long id,@RequestBody SmsFlashPromotionProductRelation smsFlashPromotionProductRelation){
        smsFlashPromotionProductRelation.setId(id);

        smsFlashPromotionProductRelationService.updateById(smsFlashPromotionProductRelation);

        return AjaxResult.success("修改成功");
    }

}
